package com.applabs.dnd.core;

import java.util.HashMap;

import android.content.Context;
import android.util.Log;

public class DNDMeProfileManager {
	
	private static final String TAG = "DNDMeProfileManager";
	private StoreData mStoreData;
	private boolean D = Constant.DEBUG;
	
	public DNDMeProfileManager(Context ctx)
	{
		mStoreData = StoreData.getInstance(ctx);
	}
	

	/* 
	 * Stores no of profiles in the storage. UI may use it!
	 * Parameters:
	 * 		isInsert : true - inserting a new profile
	 * 				   false - remove a profile
	 */
	private void manageProfileCount(boolean isInsert)
	{
		Log.i(TAG,"manage profile count enter");
		int no_of_profiles = 0;
		String temp = mStoreData.loadData(Constant.PROFILE_COUNT);
		Log.i(TAG,"load profile count");
		if (temp.equals(""))
			no_of_profiles = 0;
		else
			no_of_profiles = Integer.parseInt(temp);
		if (isInsert == true)
			no_of_profiles++;
		else
			no_of_profiles--;
		mStoreData.storeData(Constant.PROFILE_COUNT, no_of_profiles+"");
		Log.i(TAG, "manageprofile count exit");
	}
	
	public void createProfile(DNDMeProfile profile)
	{
		String data = "";
		
		if (profile == null)
		{
			Log.e(TAG,"profile is null.");
		}
		String profileName = profile.getProfileName();
		DNDCallSettings callSettings = profile.getDNDCallSettings();
		DNDMsgSettings msgSettings = profile.getDNDMsgSettings();
		
		manageProfileCount(true);
		//Serialize the data
		data += profileName+"|";
		
		data += callSettings.getDNDCallLimit()+"|";
		data += callSettings.getDNDCallWindow()+"|";
		data += callSettings.getDNDCallRejType()+"|";
		data += callSettings.getDNDCallRuletype()+"|";
		data += callSettings.getDNDCallRejectMsg()+"|";
		
		data += msgSettings.getDNDMsgRejType()+"|";
		data += msgSettings.getDNDMsgRultype()+"|";
		if(callSettings.getWhiteList() != null)
		{
			for (String number : callSettings.getWhiteList().keySet())
			{
				data += number+",";
			}
		}
		else
			Log.d(TAG, "No white list for this profile :"+profileName);
		if (D)
			Log.d(TAG,"createProfile(): Serialized data:"+data);
		
		mStoreData.storeData(Constant.PROFILE_DATA+"_"+profileName, data);
	}
	
	public void updateProfile(String profileName, DNDMeProfile profile)
	{
		removeProfile(profileName);
		createProfile(profile);
	}
	
	/**
	 * Get the profile data for a profile
	 * @param profileName : name of the profile of which data is requested
	 * @return profile object on success. On error return null
	 */
	public DNDMeProfile getProfile(String profileName)
	{
		DNDMeProfile profile = null;
		DNDCallSettings callSettings = new DNDCallSettings();
		DNDMsgSettings msgSettings = new DNDMsgSettings();
		HashMap<String,String> whiteList = new HashMap<String, String>();
		
		if (profileName == null || profileName.equals(""))
		{
			Log.e(TAG,"profileName:"+profileName+" is not valid");
			return null;
		}
		
		String temp = mStoreData.loadData(Constant.PROFILE_DATA+"_"+profileName);
		if (D)
			Log.d(TAG,"profile data retrieved:"+temp);
		if (temp.equals(""))
		{
			Log.e(TAG,"profileName:"+profileName+" is not valid");
			return null;
		}
		
		// Decode the data string.
		String[] profile_data = temp.split("\\|");
		Log.d(TAG, "profileName:"+profile_data[0]);
		if (!profile_data[0].equals(profileName))
		{
			Log.e(TAG,"profileName:"+profileName+" retrieve error.");
			return null;
		}
		Log.d(TAG, "profileCallLimit:"+profile_data[1]);
		if (profile_data[1].equals(""))
			callSettings.setDNDCallLimit(0);
		else
			callSettings.setDNDCallLimit(Integer.parseInt(profile_data[1]));
		Log.d(TAG, "profileCallWindow:"+profile_data[2]);
		if (profile_data[2].equals(""))
			callSettings.setDNDCallWindow(0);
		else
			callSettings.setDNDCallWindow(Integer.parseInt(profile_data[2]));
		Log.d(TAG, "profileCallRejType:"+profile_data[3]);
		if (profile_data[3].equals(""))
			callSettings.setDNDCallRejType(0);
		else
			callSettings.setDNDCallRejType(Integer.parseInt(profile_data[3]));
		Log.d(TAG, "profileCallRuleType:"+profile_data[4]);
		if (profile_data[4].equals(""))
			callSettings.setDNDCallRuletype(0);
		else
			callSettings.setDNDCallRuletype(Integer.parseInt(profile_data[4]));
		Log.d(TAG, "profileCallRejMsg:"+profile_data[5]);
		callSettings.setDNDCallRejectMsg(profile_data[5]);
		Log.d(TAG, "profileMsgRejType:"+profile_data[6]);
		if (profile_data[6].equals(""))
			msgSettings.setDNDMsgRejType(0);
		else
			msgSettings.setDNDMsgRejType(Integer.parseInt(profile_data[6]));
		Log.d(TAG, "profileMsgRuleType:"+profile_data[7]);
		if (profile_data[7].equals(""))
			msgSettings.setDNDMsgRuletype(0);
		else
			msgSettings.setDNDMsgRuletype(Integer.parseInt(profile_data[7]));
		try
		{
		Log.d(TAG, "profileWhiteList:"+profile_data[8]);
		for(String number : profile_data[8].split(","))
		{
			if (!number.equals(""))
				whiteList.put(number, number);
		}
		} catch (ArrayIndexOutOfBoundsException e)
		{
			Log.d(TAG, "No WhiteList");
			
		}
		profile = new DNDMeProfile(profileName, callSettings, msgSettings);
		
		return profile;
	}
	
	public void removeProfile(String profileName)
	{
		mStoreData.removeData(Constant.PROFILE_DATA+"_"+profileName);
		manageProfileCount(false);
	}
}